package com.mongodemo.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mongodemo.entity.User;
import com.mongodemo.repository.UserRepository;


@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    /**
     * 增
     * @param user
     */
    public void addUser(User user){
        userRepository.save(user);
    }
    
    /**
     * 查所有
     * @return
     */
    public List<User> getUserAll() {
    	return userRepository.findAll();
    }
    
    /**
     * 根据主键查询单个
     * @param id
     * @return
     */
    public User getUserById(Long id) {
    	return userRepository.findOne(id);
    }
    
    /**
     * 根据主键id删除
     * @param id
     * @return
     */
    public Integer delete(Long id) {
    	userRepository.delete(id);
    	return 1;
    }
    
    /**
     * 更新对象(增加和更新都是用同一个方法save)
     * @param user
     * @return
     */
    public Integer update(User user) {
    	userRepository.save(user);
    	return 1;
    }
}
